package com.doit.day01;

import java.util.*;

/**
 * 需求1：对10个订单的金额大小的升序排列
 * 需求2：对10个订单的金额大小的降序排列
 * 需求3：统计每一种品类下的订单总额
 * 需求6：找出每一种品类中金额最大的订单
 * 需求7：找出订单金额最大的两个订单
 * 需求8：找出订单金额最小的两个订单
 */

public class _03_比较器练习 {
    public static void main(String[] args) {
        ArrayList<OrderInfo> list = new ArrayList<>();
        list.add(new OrderInfo("o001", "sn001", "g001", "c001", 2, 9.9));
        list.add(new OrderInfo("o002", "sn002", "g002", "c001", 1, 1400.0));
        list.add(new OrderInfo("o003", "sn003", "g003", "c001", 3, 189.0));
        list.add(new OrderInfo("o005", "sn005", "g005", "c001", 1, 2008.0));

        list.add(new OrderInfo("o004", "sn004", "g004", "c002", 54, 800.0));
        list.add(new OrderInfo("o008", "sn008", "g008", "c002", 3, 356.0));
        list.add(new OrderInfo("o007", "sn007", "g007", "c002", 8, 19.9));
        list.add(new OrderInfo("o006", "sn006", "g006", "c002", 1, 39.9));

        //需求3：统计每一种品类下的订单总额 select sum() group by 品类
        //思路：订单总额(需要将订单的金额 * 订单数量)    收集 ==》map集合   new HashMap<品类，ArrayList<订单>>()
        /**
         *   map = new HashMap<品类，总额>()
         *   map("c001",19.8)
         *   map("c001",1400.0+19.8)
         *   map("c001",567+1400.0+19.8)
         *   map("c001",2008+567+1400.0+19.8)
         *   map("c002",800.0*54+356.0*3+8*19.9+39.9)
         *   循环累加
         */
       /* HashMap<String, Double> map = new HashMap<>();
        for (OrderInfo orderInfo : list) {
            //第一次get出来可没有值哦
            OrderInfo getOrderInfo = map.get(orderInfo.getCategory_id());
            //代表不在集合里面
            if(value != null)
                map.put(orderInfo.getCategory_id(),value+orderInfo.getPrice()*orderInfo.getGoods_number());
        }
        //循环走完之后，map中有没有全部求出 正确结果
        System.out.println(map);*/

        //需求6：找出每一种品类中金额最大的订单  还是返回两条数据   group by 品类 求max(针对订单金额)
        // c001  OrderInfo("o005", "sn005", "g005", "c001", 1, 2008.0)
        // c002  OrderInfo("o004", "sn004", "g004", "c002", 54, 800.0)




    }

    // 需求1：对10个订单的金额大小的升序排列
    public static void xuqiu1(ArrayList<OrderInfo> list) {
        Collections.sort(list, new Comparator<OrderInfo>() {
            @Override
            public int compare(OrderInfo o1, OrderInfo o2) {
                return Double.compare(o1.getGoods_number() * o1.getPrice(), o2.getGoods_number() * o2.getPrice());
            }
        });
        for (OrderInfo orderInfo : list) {
            System.out.println(orderInfo);
        }
    }

    // 需求2：对10个订单的金额大小的降序排列
    public static void xuqiu2(ArrayList<OrderInfo> list) {
        Collections.sort(list, new Comparator<OrderInfo>() {
            @Override
            public int compare(OrderInfo o1, OrderInfo o2) {
                return -Double.compare(o1.getGoods_number() * o1.getPrice(), o2.getGoods_number() * o2.getPrice());
            }
        });
        for (OrderInfo orderInfo : list) {
            System.out.println(orderInfo);
        }
    }

    //需求3：统计每一种品类下的订单总额
    public static void xuqiu3(ArrayList<OrderInfo> list) {
        //统计品类的总额，需要将品类进行分组，然后订单总额都加起来
        HashMap<String, Double> map = new HashMap<>();

        //遍历每一个订单，获取品类，丢到hashMap中
        for (OrderInfo orderInfo : list) {
            //去map中查看是否有之前的品类
            Double value = map.getOrDefault(orderInfo.getCategory_id(), 0.0) + orderInfo.getPrice() * orderInfo.getGoods_number();
            map.put(orderInfo.getCategory_id(), value);
        }

        Set<String> set = map.keySet();
        for (String key : set) {
            Double totalPrice = map.get(key);
            System.out.println(key + "==>" + totalPrice);
        }
    }

    //需求6：找出每一种品类中金额最大的订单
    public static void xuqiu6(ArrayList<OrderInfo> list) {
        //需要返回的是品类中金额最大的订单，返回的是订单，所以需要将订单都收集起来
        HashMap<String, ArrayList<OrderInfo>> map = new HashMap<>();

        //遍历每一个订单，获取品类，丢到hashMap中
        for (OrderInfo orderInfo : list) {
            //去map中查看是否有之前的品类
            ArrayList<OrderInfo> orders = map.getOrDefault(orderInfo.getCategory_id(), new ArrayList<OrderInfo>());
            orders.add(orderInfo);
            map.put(orderInfo.getCategory_id(),orders);
        }

        Set<String> set = map.keySet();
        for (String key : set) {
            ArrayList<OrderInfo> orderInfos = map.get(key);
            Collections.sort(orderInfos, new Comparator<OrderInfo>() {
                @Override
                public int compare(OrderInfo o1, OrderInfo o2) {
                    return Double.compare(o2.getGoods_number() * o2.getPrice(),o1.getGoods_number() * o1.getPrice());
                }
            });
            OrderInfo orderInfo = orderInfos.get(0);
            System.out.println(key+"=>"+orderInfo);
        }

    }

    //需求7：找出订单金额最大的两个订单
    public static void xuqiu7(ArrayList<OrderInfo> list) {
        Collections.sort(list, new Comparator<OrderInfo>() {
            @Override
            public int compare(OrderInfo o1, OrderInfo o2) {
                return Double.compare(o2.getGoods_number() * o2.getPrice(),o1.getGoods_number() * o1.getPrice());
            }
        });

        for (int i = 0; i < 2; i++) {
            System.out.println(list.get(i));
        }
    }

    //需求7：找出订单金额最小的两个订单
    public static void xuqiu8(ArrayList<OrderInfo> list) {
        Collections.sort(list, new Comparator<OrderInfo>() {
            @Override
            public int compare(OrderInfo o1, OrderInfo o2) {
                return -Double.compare(o2.getGoods_number() * o2.getPrice(),o1.getGoods_number() * o1.getPrice());
            }
        });

        for (int i = 0; i < 2; i++) {
            System.out.println(list.get(i));
        }
    }
}
